<template>
	<view class="page">
		<template v-if="isLogin">
			<view class="body"> 
				<template v-if="loadingState.default">
					<loading :loadingState="loadingState"></loading>
				</template>
				
				<template v-else>
					<view v-if="list.length>0" class="detail-wrap m-b2">
						<template> 
							<label class="check-item" v-for="(item,index) in list" :key="index" @click="setAddMember(item.id,index)">
								<view class="text">{{item.realname}}</view>
								<checkbox class="radio" :value="item.id" color="#f00"/>
							</label> 
							<loading :loadingState="loadingState"></loading> 
						</template>
						
					</view>
					<template v-else>
						<empty msg="暂无可添加人员"></empty>
					</template>
				</template>
				
				 
			</view> 
			
			<view v-if="list.length>0" class="footer">
				<view class="fix-wrap bottom">
					<view class="form-lay-btn" @click="addMember" >添加</view>
				</view>
			</view> 
		</template>
	</view>
</template>

<script> 
import loading from '@/components/loading-more.vue'
import empty from '@/components/empty.vue'
	export default {
		name: 'name',
		data() {
			return { 
				isLogin: false, 
				userInfo: uni.getStorageSync('userInfo'), 
				loadingState:{
					default:true,
					loading:false,
					compelted:false
				}, 
				isStop:true,
				list:[],
				params:{
					pid: '',
					select_id:[], 
					params:[]
				}

			}


		},
		async onLoad(params) {
			this.params.pid=params.pid;
			this.isLogin=await this.$loginState('check');  
			if(this.isLogin){ 
				this.getMemberList();
				
			}
		},
		async onShow() {

		},
		methods: {
			
			// 获取可添加人员名单
			getMemberList:async function(){
				let res=await this.$api.pro_api.getMemberList({
					pid:this.params.pid
				})  
				console.log(res)
				if(res.data.ret==0){
					this.loadingState.default=false;
					this.list=res.data.data;
				}else{
					this.loadingState.default=false;
				}
			},
			
			// 设置选中参数
			setAddMember:function(id,idx){  
				let datas=[];
				if(this.params.select_id.indexOf(id)==-1){
					this.params.select_id.push(id);
				}else{
					let index=this.params.select_id.indexOf(id);
					this.params.select_id.splice(index,1);
				}
				
				this.params.select_id.forEach(item=>{ 					
					datas.push([3, item, this.params.pid, 1, this.userInfo.id]);
					
				}) 				
				this.params.params=datas; 
			},
			
			// 提交数据
			addMember:async function(){
				let _this=this;
				if(this.params.params.length==0){
					uni.showToast({
						icon:"none",
						title:"请至少选择一名成员",
						mask:true
					})
					return 
				}
				 
				if(this.isStop){
					this.isStop=false;
					
					uni.showLoading({
						mask:true,
						title:'提交中...'
					})
					
					let res=await this.$api.pro_api.addMemberList({
						datas:JSON.stringify(this.params.params)
					})  
					if(res.data.ret==0){
						uni.hideLoading(); 
						uni.showToast({
							icon:"success",
							title:"添加成功",
							mask:true,
							duration:1000,
							success() {
								setTimeout(()=>{
									_this.isStop=true;
									_this.$store.commit('UPDATE_PRO_JC_MEMBER',1);      // 添加成功更新项目成员列表
									uni.navigateBack({
										delta:1
									})
								},1000)
							}
						})
					}else{
						uni.hideLoading();
						uni.showToast({
							icon:"none",
							image:"../../static/images/error.png",
							title:res.data.message,
							mask:true,
							duration:1000,
							success() {
								setTimeout(()=>{
									_this.isStop=true;
								},1000)
							}
						})
					}
				}
				
			} 
		},
		components: {
			loading,
			empty
		},

		computed: {

		},
		watch: {

		}

	}
</script>

<style lang="less" scoped> 
	  
	.label-area{
		width: 95%;
		display: flex;
		justify-content: space-between;
		padding-right:5%;
		.get-local-btn{	
			display: flex;
			justify-content: center;
			align-items: center; 
			padding:0 10rpx;
			background: #f00; 
			border-radius: 10rpx; 
			font-size: 30rpx;
			color:#fff; 
		}
	}
	
	
</style>
